package com.syx.friend.Controller.user;

import com.syx.commoncore.Constant.HttpConstants;
import com.syx.commoncore.Model.Result;
import com.syx.commoncore.enums.Resultcode;
import com.syx.friend.Model.user.DTO.UserDTO;
import com.syx.friend.Model.user.DTO.UserUpdateDTO;
import com.syx.friend.Model.user.VO.UserVO;
import com.syx.friend.Service.IUserService;
import com.syx.security.Exception.ServiceException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequestMapping("/user")
@RestController
public class UserController {
    final static Pattern partern = Pattern.compile("[a-zA-Z0-9]+[\\.]{0,1}[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z]+");

    @Resource
    IUserService iUserService;


    /// user/sendCode
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userDTO){
        boolean isMatch = partern.matcher(userDTO.getEmail()).matches();
        if (!isMatch){
            throw new ServiceException(Resultcode.EMAIL_FAILURE);
        }


        return iUserService.sendCode(userDTO);

    }
    @GetMapping("/emailLogin")
    public Result emailLogin(UserDTO userDTO){
        boolean isMatch = partern.matcher(userDTO.getEmail()).matches();
        // 查看是否存在这一个qq邮箱
        if (!isMatch){
            throw new ServiceException(Resultcode.EMAIL_FAILURE);
        }
        return iUserService.emailLogin(userDTO);

    }
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return iUserService.logout(token);


    }
    @RequestMapping("/info")
    public Result<UserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return iUserService.info(token);
    }
    @GetMapping("/detail")
    public Result<UserVO> detail(){
        return iUserService.detail();
    }



    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO){
        return iUserService.edit(userUpdateDTO);
    }


    @PutMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO){
        return iUserService.updateHeadImage(userUpdateDTO.getHeadImage());

    }



    @RequestMapping("test")
    public Result<Void> test(){
        return null;
    }





//    @GetMapping("/test1")
//    public Result test(){
//        return Result.success(Resultcode.SUCCESS);
//    }







}
